package com.ibl.rover.lite.handler;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.ibl.rover.lite.handler.AppSetupHandler;
import com.ibl.rover.lite.handler.CustomerHandler;
import com.ibl.rover.lite.service.utill.Core;
import com.ibl.rover.lite.service.utill.Core.COMMON_TAGS;
import com.ibl.rover.lite.service.utill.Core.CUSTOMER;
import com.ibl.rover.lite.service.utill.Core.SST_TAGS;
import com.ibl.rover.lite.service.utill.Core.USER_TAGS;

import java.util.ArrayList;
import java.util.List;

import com.ibl.rover.lite.service.utill.HibernateUtill;
import com.ibl.rover.lite.services.sync.request.ISyncRequest;
import com.rover.lite.hibernate.dal.RvrAppsetup;
import com.rover.lite.hibernate.dal.RvrCustomers;
import com.rover.lite.hibernate.dal.RvrCustomersId;
import com.rover.lite.hibernate.dal.RvrPrdcategory;
import com.rover.lite.hibernate.dal.RvrRouteqry;
import com.ibl.rover.lite.service.utill.HibernateUtill;
import com.rover.lite.hibernate.dal.RvrCustomers;

public class CustomerHandler {

	Session session;
	Document doc;

	public CustomerHandler(Document _doc) {
		// TODO Auto-generated constructor stub
		session = HibernateUtill.getSessionFactory().openSession();
		doc = _doc;

	}

	public void getCustomers(Element bodyElement, String userName) {

		Criteria criteria = session.createCriteria(RvrPrdcategory.class);

		Criteria rvr_RouteQryCriteria = session
				.createCriteria(RvrRouteqry.class);

		rvr_RouteQryCriteria.add(Restrictions.eq("id.tsoCode", userName));

		List<RvrRouteqry> rvrRouteqryList = new ArrayList<RvrRouteqry>();

		rvrRouteqryList = rvr_RouteQryCriteria.list();
		Element entityElement = doc.createElement("entity");
		entityElement.setAttribute("name", SST_TAGS.CUSTOMERS);
		bodyElement.appendChild(entityElement);

		Element addElement = doc.createElement("add");
		entityElement.appendChild(addElement);
		for (RvrRouteqry rvrRouteqry : rvrRouteqryList) {

			List<RvrCustomers> rvrCustomerList = new ArrayList<RvrCustomers>();
			Criteria rvrCustomersCriteria = session
					.createCriteria(RvrCustomers.class);
			rvrCustomersCriteria.add(Restrictions.eq("id.vsrCode", userName));
			rvrCustomersCriteria.add(Restrictions.eq("id.rouNumber",
					rvrRouteqry.getId().getRouNumber()));
			rvrCustomerList = rvrCustomersCriteria.list();

			for (RvrCustomers rvrCustomers : rvrCustomerList) {
				if (rvrCustomers != null) {
					Element itemElement = doc.createElement("item");
					addElement.appendChild(itemElement);
					setRvrAppsetupAttribute(rvrCustomers, itemElement);
				}

			}
		}
	}

	private void setRvrAppsetupAttribute(RvrCustomers rvrCustomers,
			Element itemElement) {

		try {
			ArrayList<String> columnName = new ArrayList<String>();
			ArrayList<String> columnValue = new ArrayList<String>();
			//
			columnName.add(CUSTOMER.ADDRESS);
			columnValue.add(String.valueOf(rvrCustomers.getId().getAddress()));

			columnName.add(CUSTOMER.CLASS_CODE);
			columnValue
					.add(String.valueOf(rvrCustomers.getId().getClassCode()));

			columnName.add(CUSTOMER.CODE);
			columnValue.add(String.valueOf(rvrCustomers.getId().getCode()));

			columnName.add(CUSTOMER.CREDIT_LIMIT);
			columnValue.add(String.valueOf(rvrCustomers.getCreditLimit()));

			columnName.add(CUSTOMER.NAME);
			columnValue.add(String.valueOf(rvrCustomers.getId().getName()));

			columnName.add(CUSTOMER.ROU_NUMBER);
			columnValue
					.add(String.valueOf(rvrCustomers.getId().getRouNumber()));
			
			columnName.add(CUSTOMER.POSITION);
			columnValue
					.add(String.valueOf(rvrCustomers.getId().getPosition()));

			Core.setAttribute(columnName, columnValue, itemElement, doc);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}



